<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>医生转诊详情</title>
    <style>
    	.division_row {
        	height:50px;
        }
		#in-patient th,
		#in-patient td,
		#out-patient th,
		#out-patient td{
        	text-align: center;
        }
		.ui-jqgrid tr.jqgrow td {
			white-space: normal;
			word-break: break-word;
		}
    </style>
</head>
<body>
	<div class="row">
	     <div class="col-sm-12">
	     	<div class="col-sm-6">
	     		<h4>选择医院</h4>
	     		<select id="select-hospital" class="form-control">
	     		
	     		</select>
	     	</div>
	     	<div class="col-sm-6">
	     		<h4>选择医生</h4>
	     		<select id="select-medical" class="form-control">
	     		
	     		</select>
	     	</div>
	     </div>
	</div>
	<div class="division_row"></div>
	<div class="row">
	 	<div class="col-sm-12">
	 		<h3>转入患者总数(人) &nbsp;<span id="in-num">0</span></h3>
	 	</div>
	 	<!--<div class="col-sm-12 table-responsive">-->
	 		<!--<table id="in-count" class="doctor-count table table-bordered">-->
				<!--<thead>-->
					<!--<tr>-->
						<!--<th>转诊病历号</th>-->
						<!--<th>姓名</th>-->
						<!--<th>性别</th>-->
						<!--<th>年龄</th>-->
						<!--<th>诊断</th>-->
						<!--<th>创建时间</th>-->
						<!--<th>操作</th>-->
					<!--</tr>-->
				<!--</thead>-->
				<!--<tbody>-->
					<!--<tr>-->
						<!--<td>1564641547</td>-->
						<!--<td>郭靖</td>-->
						<!--<td>男</td>-->
						<!--<td>40</td>-->
						<!--<td>脑性瘫痪</td>-->
						<!--<td>2017-07-26 15:08</td>-->
						<!--<td><a onclick="setParamToUrl({'emr_id':1000});loadTitle('/emr/info-title.html');">病历</a></td>-->
					<!--</tr>-->
				<!--</tbody>-->
			<!--</table>-->
	 	<!--</div>-->

		 <div class="col-sm-12">
			 <div class="jqGrid_wrapper" id="in-patient-list" style="text-align: center;width: 100%;">
				 <div id="in-patient-div" style="text-align: center;width: 100%;">
					 <table id="in-patient" style="text-align: center;width: 100%;"></table>
				 </div>
				 <div id="no-records-1" class="hide">没有查询记录！</div>
				 <div class="pagination hide" id="pagination-1">
					 <a class="first" id="first-btn-1" data-action="first"><i class="icon-double-angle-left"></i> </a>
					 <a class="previous" id="previous-btn-1" data-action="previous"><i class="icon-angle-left"></i> </a>
					 <input id="page-num-1" type="text" onkeydown="designatedPage(event, 1)"/>
					 <a class="next" id="next-btn-1" data-action="next"><i class="icon-angle-right"></i> </a>
					 <a class="last" id="last_btn_1" data-action="last"><i class="icon-double-angle-right"></i> </a>
				 </div>
			 </div>
		 </div>
	</div>
	<div class="division_row"></div>
	<div class="row">
	 	<div class="col-sm-12">
	 		<h3>转出患者总数(人) &nbsp;<span id="out-num">0</span></h3>
	 	</div>
	 	<!--<div class="col-sm-12 table-responsive">
	 		<table id="out-count" class="doctor-count table table-bordered">
				<thead>
					<tr>
						<th>转诊病历号</th>
						<th>姓名</th>
						<th>性别</th>
						<th>年龄</th>
						<th>诊断</th>
						<th>创建时间</th>
						<th>操作</th>
					</tr>
				</thead>
				<tbody>
					<tr>
						<td>1564641547</td>
						<td>郭靖</td>
						<td>男</td>
						<td>40</td>
						<td>脑性瘫痪</td>
						<td>2017-07-26 15:08</td>
						<td><a onclick="setParamToUrl({'emr_id':1000});loadTitle('/emr/info-title.html');">病历</a></td>
					</tr>
				</tbody>
			</table>
	 	</div>-->
		<div class="col-sm-12">
			<div class="jqGrid_wrapper" id="out-patient-list" style="text-align: center;width: 100%;">
				<div id="out-patient-div" style="text-align: center;width: 100%;">
					<table id="out-patient" style="text-align: center;width: 100%;"></table>
				</div>
				<div id="no-records-2" class="hide">没有查询记录！</div>
				<div class="pagination hide" id="pagination-2">
					<a class="first" id="first-btn-2" data-action="first"><i class="icon-double-angle-left"></i> </a>
					<a class="previous" id="previous-btn-2" data-action="previous"><i class="icon-angle-left"></i> </a>
					<input id="page-num-2" type="text" onkeydown="designatedPage(event, 1)"/>
					<a class="next" id="next-btn-2" data-action="next"><i class="icon-angle-right"></i> </a>
					<a class="last" id="last_btn_2" data-action="last"><i class="icon-double-angle-right"></i> </a>
				</div>
			</div>
		</div>
	</div>

<script>
	var medicalId = getUrlParam("medical_id"),
		hospitalId = getUrlParam("hospital_id");

    var currentPage1 = 1,
    	totalPage1 = 1,
    	currentPage2 = 1,
    	totalPage2 = 1,
    	pageSize = 10;

    var inPatientDivHtml = $("#in-patient-div"),
    	outPatientDivHtml = $("#out-patient-div");

    var pagination1 = $('#pagination-1'),
    	noRecords1 = $("#no-records-1"),
    	pagination2 = $('#pagination-2'),
    	noRecords2 = $("#no-records-2");



    //初始化页面
    function pageInit() {
        getHospital();
        getDoctor(hospitalId);
        getData(medicalId,hospitalId);

        pagination1.jqPagination({
            max_page: totalPage1,
            current_page: currentPage1,
            page_string: '当前第{current_page}页,共{max_page}页',
            paged: function (page) {
                if (page != currentPage1) {
                    getIutEmr(dataLists.in_data);
                    currentPage1 = page;
                }
            }
        });
        pagination2.jqPagination({
            max_page: totalPage2,
            current_page: currentPage2,
            page_string: '当前第{current_page}页,共{max_page}页',
            paged: function (page) {
                if (page != currentPage2) {
                    //getOutEmr(dataLists.out_data);
                    currentPage2 = page;
                }
            }
        });
        $('#in-patient-list').bind('resize', function () {
            var width = $('#in-patient-list').width();
            $("#in-patient").setGridWidth(width);
        });
        $('#out-patient-list').bind('resize', function () {
            var width = $('#out-patient-list').width();
            $("#out-patient").setGridWidth(width);
        });
    }
    //pageInit();


    $('#in-patient-list').bind('resize', function () {
        var width = $('#in-patient-list').width();
        $("#in-patient").setGridWidth(width);
    });
    $('#out-patient-list').bind('resize', function () {
        var width = $('#out-patient-list').width();
        $("#out-patient").setGridWidth(width);
    });
    $("#select-hospital,#select-medical").change(function(){
        var medicalId = $("#select-medical option:selected").val();
        var hospitalId = $("#select-hospital option:selected").val();
        getData(medicalId,hospitalId)
	})

    getHospital();
    getDoctor(hospitalId);
    getData(medicalId,hospitalId);



    //转出患者列表
    function getOuttEmr(out_data) {
        /*currentPage2 = data.current_page;
        totalPage2 = data.total_page;
        if (totalPage2 == 0) {
            totalPage2 = 1;
        }
        pagination2.jqPagination('option', 'current_page', currentPage2);
        pagination2.jqPagination('option', 'max_page', totalPage2);
        if (data.list == null) {
            data.list = [];
            noRecords2.show();
            pagination2.hide();
        }
        else if (data.list.length != 0) {
            noRecords2.hide();
            pagination2.show();
        }
        else {
            noRecords2.show();
            pagination2.hide();
        }*/
        outPatientDivHtml.empty();
        outPatientDivHtml.append("<table id='out-patient' class=''></table>");

        $("#out-patient").jqGrid({
            data: out_data,
            datatype: "local",
            height: "auto",
            autowidth: true,
            shrinkToFit: true,
            rowNum: pageSize,
            viewrecords: true,
            hidegrid: false,
            rownumbers: true,
            colNames: [ '转诊病历号', '姓名', '性别', '年龄','诊断', '创建时间', '查看'],
            colModel: [
                {name: 'out_emr.number'},
                {name: 'out_emr.patient.name'},
                {
                    name: 'out_emr.patient.sex', index: 'out_emr.patient.sex',
                    formatter: function (cellvalue, options, rowObject) {
                        return getSex(cellvalue);
                    }
                },
                {
                    name: 'out_emr.patient.birthday', index: 'out_emr.patient.birthday',
                    formatter: function (cellvalue, options, rowObject) {
                        return setAge(cellvalue);
                    }
                },
                {
                    name: 'out_emr.principal_diagnosis', index: 'out_emr.principal_diagnosis',
                    formatter: function (cellvalue, options, rowObject) {
                        if (cellvalue == "null" || !cellvalue){
                            return "";
                        }else {
                            return cellvalue;
                        }
                    }
                },
                {
                    name: 'out_emr.create_time', index: 'out_emr.create_time',
                    formatter: function (cellvalue, options, rowObject) {
                        return  new Date(cellvalue).format("YYYY-MM-DD HH:mm");
                    }
                },
                {
                    name: 'lookup', index: 'lookup',
                    formatter: function (cellvalue, options, rowObject) {
                        var param = {};
                        param.emr_id = rowObject.out_emr.id;
                        param.transfer_id = rowObject.id;
                        param.type = 0;
                        var button = $("<button class='btn btn-primary btn-xs'>病历</button>");
                        button.attr("onclick", "setParamToUrl(" + JSON.stringify(param) + ");loadTitle('/emr/info-title.html')");
                        return button.prop("outerHTML");
                    }
                }
            ]
        });
        $("#out-patient tr:even").css("background","#f6f6f6")
    }

    //转入患者列表
    function getIutEmr(in_data) {
        /*currentPage1 = data.current_page;
        totalPage1 = data.total_page;
        if (totalPage1 == 0) {
            totalPage1 = 1;
        }
        pagination1.jqPagination('option', 'current_page', currentPage1);
        pagination1.jqPagination('option', 'max_page', totalPage1);
        if (data.list == null) {
            data.list = [];
            noRecords1.show();
            pagination1.hide();
        }
        else if (data.list.length != 0) {
            noRecords1.hide();
            pagination1.show();
        }
        else {
            noRecords1.show();
            pagination1.hide();
        }*/
        inPatientDivHtml.empty();
        inPatientDivHtml.append("<table id='in-patient' class=''></table>");

        $("#in-patient").jqGrid({
            data: in_data,
            datatype: "local",
            height: "auto",
            autowidth: true,
            shrinkToFit: true,
            rowNum: pageSize,
            viewrecords: true,
            hidegrid: false,
            rownumbers: true,
            colNames: [ '转诊病历号', '姓名', '性别', '年龄','诊断', '创建时间', '查看'],
            colModel: [
                {name: 'in_emr.number'},
                {name: 'in_emr.patient.name'},
                {
                    name: 'in_emr.patient.sex', index: 'in_emr.patient.sex',
                    formatter: function (cellvalue, options, rowObject) {
                        return getSex(cellvalue);
                    }
                },
                {
                    name: 'in_emr.patient.birthday', index: 'in_emr.patient.birthday',
                    formatter: function (cellvalue, options, rowObject) {
                        return setAge(cellvalue);
                    }
                },
                {
                    name: 'in_emr.principal_diagnosis', index: 'in_emr.principal_diagnosis',
                    formatter: function (cellvalue, options, rowObject) {
                        if (cellvalue == "null" || !cellvalue){
                            return "";
                        }else {
                            return cellvalue;
                        }
                    }
                },
                {
                    name: 'in_emr.create_time', index: 'in_emr.create_time',
                    formatter: function (cellvalue, options, rowObject) {
                        return  new Date(cellvalue).format("YYYY-MM-DD HH:mm");
                    }
                },
                {
                    name: 'lookup', index: 'lookup',
                    formatter: function (cellvalue, options, rowObject) {
                        var param = {};
                        param.emr_id = rowObject.in_emr.id;
                        param.transfer_id = rowObject.id;
                        param.type = 1;
                        var button = $("<button class='btn btn-primary btn-xs'>病历</button>");
                        button.attr("onclick", "setParamToUrl(" + JSON.stringify(param) + ");loadTitle('/emr/info-title.html')");
                        return button.prop("outerHTML");
                    }
                }
            ]
        });
        $("#in-patient tr:even").css("background","#f6f6f6")
    }

    //获取列表数据
	function getData(medicalId,hospitalId){
        var dataLists = {};
        dataLists.in_data = [];
        dataLists.out_data = [];
       	mandy_statistics_referral_by_patient(medicalId,hospitalId,function(data){
            console.log("++++++++++转诊统计按医院及医生查询++++++++++")
            console.log(data)
            console.log("++++++++++转诊统计按医院及医生查询++++++++++")
			$("#in-num").text(data.map.inCount);
			$("#out-num").text(data.map.OutCount);
            dataLists.in_data = data.map.in_referral;
            dataLists.out_data = data.map.out_referral;
            getIutEmr(dataLists.in_data);
            getOuttEmr(dataLists.out_data)
		},function(err){
            console.log(err)
		})
	}

    //获取医院列表
    function getHospital(){
        mandy_get_hospitals(function(data){
            console.log("++++++++++转诊统计详情医院列表++++++++++")
            console.log(data)
            console.log("++++++++++转诊统计详情医院列表++++++++++")
            var lists = data.list;
            $.each(lists,function(index,list){
                var option = $("<option value='" + list.id + "'>" + list.name + "</option>");
                $("#select-hospital").append(option);
            })
            $("#select-hospital option[value = " + hospitalId + "]").prop("selected",true);
        },function(err){
            console.log(err)
        })
    }

    //获取医生列表
	function getDoctor(hospitalId){
        mandy_get_doctor_staff_list(hospitalId,function(data){
            console.log("++++++++++医生列表++++++++++")
            console.log(data)
            console.log("++++++++++医生列表++++++++++")
            var lists = data.medical_staff;
			$.each(lists,function(idx,list){
                var option = $("<option value='" + list.profile_info.id + "'>" + list.profile_info.name + "</option>");
                $("#select-medical").append(option);
			})
            $("#select-medical option[value = " + medicalId + "]").prop("selected",true);
		},function(err){
            console.log(err)
		})

	}
</script>
</body>
</html>
